package com.example.service.impl;

import com.example.service.ImportTaskService;
import com.example.service.util.ImportTask;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

/**
 * @Description: 异步方式导入数据
 * @Author runto
 * @Date 2023/10/14 12:12
 * @PackageName:com.example.service.impl
 * @ClassName: ImportTaskService
 * @Version 1.0
 */
@Service
public class ImportTaskServiceImpl implements ImportTaskService {

    @Override
    public String syncImport() throws Exception {
        //起后台线程做导入
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        ExecutorService threadPool = new ThreadPoolExecutor(1, 3,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());
        ImportTask importTask=new ImportTask();
        threadPool.execute(importTask);
        threadPool.shutdown();
        return "正在导入中，请到异步队列查询结果";
    }
}
